package cn.melonlib.cores.aop.comp.anno;

import cn.melonlib.cores.aop.anno.PrependUrl;
import cn.melonlib.cores.aop.anno.inject.*;
import cn.melonlib.cores.aop.anno.result.TransToView;
import cn.melonlib.cores.aop.comp.inject.*;
import cn.melonlib.cores.aop.comp.result.TransToViewInterceptor;
import cn.melonlib.cores.aop.comp.url.PrependUrlInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.annotation.Resource;

@Configuration
@DependsOn({"prependUrlInterceptor",
        "injectRecordInterceptor",
        "injectTenantInterceptor",
        "filterTenantInterceptor",
        "filterRecordInterceptor",
        "transToViewInterceptor",
        })
public class InterceptorAnnotationConfig {

    @Resource
    private PrependUrlInterceptor prependUrlInterceptor;

    @Resource
    private InjectRecordInterceptor injectRecordInterceptor;

    @Resource
    private InjectTenantInterceptor injectTenantInterceptor;

    @Resource
    private FilterTenantInterceptor filterTenantInterceptor;

    @Resource
    private FilterRemovedInterceptor filterRemovedInterceptor;

    @Resource
    private FilterRecordInterceptor filterRecordInterceptor;

    @Resource
    private TransToViewInterceptor transToViewInterceptor;

    @Bean
    public DefaultPointcutAdvisor prependUrl(){
        AnnotationMatchingPointcut pointcut=new AnnotationMatchingPointcut(PrependUrl.class,true);
        DefaultPointcutAdvisor defaultPointcutAdvisor=new DefaultPointcutAdvisor();
        defaultPointcutAdvisor.setPointcut(pointcut);
        defaultPointcutAdvisor.setAdvice(prependUrlInterceptor);
        defaultPointcutAdvisor.setOrder(10);
        return defaultPointcutAdvisor;
    }

    @Bean
    public DefaultPointcutAdvisor injectRecord(){
        AnnotationMatchingPointcut pointcut=new AnnotationMatchingPointcut(InjectRecord.class,true);
        DefaultPointcutAdvisor defaultPointcutAdvisor=new DefaultPointcutAdvisor();
        defaultPointcutAdvisor.setPointcut(pointcut);
        defaultPointcutAdvisor.setAdvice(injectRecordInterceptor);
        defaultPointcutAdvisor.setOrder(10);
        return defaultPointcutAdvisor;
    }

    @Bean
    public DefaultPointcutAdvisor injectTenant(){
        AnnotationMatchingPointcut pointcut=new AnnotationMatchingPointcut(InjectTenant.class,true);
        DefaultPointcutAdvisor defaultPointcutAdvisor=new DefaultPointcutAdvisor();
        defaultPointcutAdvisor.setPointcut(pointcut);
        defaultPointcutAdvisor.setAdvice(injectTenantInterceptor);
        defaultPointcutAdvisor.setOrder(10);
        return defaultPointcutAdvisor;
    }

    @Bean
    public DefaultPointcutAdvisor filterTenant(){
        AnnotationMatchingPointcut pointcut=new AnnotationMatchingPointcut(FilterTenant.class,true);
        DefaultPointcutAdvisor defaultPointcutAdvisor=new DefaultPointcutAdvisor();
        defaultPointcutAdvisor.setPointcut(pointcut);
        defaultPointcutAdvisor.setAdvice(filterTenantInterceptor);
        defaultPointcutAdvisor.setOrder(10);
        return defaultPointcutAdvisor;
    }

    @Bean
    public DefaultPointcutAdvisor filterRemoved(){
        AnnotationMatchingPointcut pointcut=new AnnotationMatchingPointcut(FilterRemoved.class,true);
        DefaultPointcutAdvisor defaultPointcutAdvisor=new DefaultPointcutAdvisor();
        defaultPointcutAdvisor.setPointcut(pointcut);
        defaultPointcutAdvisor.setAdvice(filterRemovedInterceptor);
        defaultPointcutAdvisor.setOrder(10);
        return defaultPointcutAdvisor;
    }

    @Bean
    public DefaultPointcutAdvisor filterRecord(){
        AnnotationMatchingPointcut pointcut=new AnnotationMatchingPointcut(FilterRecord.class,true);
        DefaultPointcutAdvisor defaultPointcutAdvisor=new DefaultPointcutAdvisor();
        defaultPointcutAdvisor.setPointcut(pointcut);
        defaultPointcutAdvisor.setAdvice(filterRecordInterceptor);
        defaultPointcutAdvisor.setOrder(10);
        return defaultPointcutAdvisor;
    }

    @Bean
    public  DefaultPointcutAdvisor transToView(){
        AnnotationMatchingPointcut pointcut=new AnnotationMatchingPointcut(TransToView.class,true);
        DefaultPointcutAdvisor defaultPointcutAdvisor=new DefaultPointcutAdvisor();
        defaultPointcutAdvisor.setPointcut(pointcut);
        defaultPointcutAdvisor.setAdvice(transToViewInterceptor);
        defaultPointcutAdvisor.setOrder(10);
        return defaultPointcutAdvisor;
    }

}
